package com.tanhua.commons.templates;

import com.tanhua.commons.properties.TencentCloudProperties;
import com.tencentcloudapi.ams.v20201229.AmsClient;
import com.tencentcloudapi.ams.v20201229.models.*;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;

public class TencentCloudTemplate {

    private TencentCloudProperties properties;

    public TencentCloudTemplate(TencentCloudProperties properties){
        this.properties = properties;
    }

    public String soundSend(String soundUrl){
        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(properties.getSecretId(), properties.getSecretKey());
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ams.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            AmsClient client = new AmsClient(cred, "", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            CreateAudioModerationTaskRequest req = new CreateAudioModerationTaskRequest();
            TaskInput[] taskInputs = new TaskInput[1];
            TaskInput taskInput = new TaskInput();
            //taskInput.setDataId("123");
            //taskInput.setName("tanhua");
            StorageInfo storageInfo = new StorageInfo();
            storageInfo.setUrl(soundUrl);
            //storageInfo.setUrl("https://tanhuadzstar.oss-cn-guangzhou.aliyuncs.com/images/2021/11/09/981c0887-7369-4e8d-ba80-278a58b254c1.mp3");
            taskInput.setInput(storageInfo);

            taskInputs[0] = taskInput;

            req.setTasks(taskInputs);

            // 返回的resp是一个CreateAudioModerationTaskResponse的实例，与请求对象对应
            CreateAudioModerationTaskResponse resp = client.CreateAudioModerationTask(req);

            // 返回taskId用于获得检测结果
            TaskResult[] results = resp.getResults();
            TaskResult result = results[0];
            String taskId = result.getTaskId();

            // 输出json格式的字符串回包
            System.out.println(CreateAudioModerationTaskResponse.toJsonString(resp));
            return taskId;
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return null;
    }

    public Boolean soundCheck(String taskId){
        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(properties.getSecretId(), properties.getSecretKey());
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ams.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            AmsClient client = new AmsClient(cred, "", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeTaskDetailRequest req = new DescribeTaskDetailRequest();
            req.setTaskId(taskId);
            // 返回的resp是一个DescribeTaskDetailResponse的实例，与请求对象对应
            DescribeTaskDetailResponse resp = client.DescribeTaskDetail(req);
            // 输出json格式的字符串回包
            System.out.println(DescribeTaskDetailResponse.toJsonString(resp));

            String suggestion = resp.getSuggestion();

            //返回审核结果
            return suggestion.equals("Pass");
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return null;
    }
}
